package com.example.exception;

import com.example.pojo.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public <T> Response<T> handleGlobalException(Exception e){
        log.error(e.getMessage(),e);
        return Response.fail(StringUtils.hasLength(e.getMessage())? e.getMessage():"操作失败");
    }

    @ExceptionHandler(BusinessException.class)
    public <T> Response<T> handleBusinessException(BusinessException e){
        log.error(e.getMessage(),e);
        return Response.fail(e.getMessage());
    }
}
